//
//  HistoryViewController.m
//  YYPHSpeedJump
//
//  Created by sujiang on 15/12/28.
//  Copyright © 2015年 sujiang. All rights reserved.
//

#import "HistoryViewController.h"
#import "HistoryCell.h"

@interface HistoryViewController ()<UITableViewDataSource, UITableViewDelegate>
{
    NSInteger page;
}

@property (nonatomic, strong) UITableView *tableVeiw;

@end

@implementation HistoryViewController
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    page = 1;
    [self addSubViews];
    [self requestData];
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)requestData
{
    NSString *url = [NSString stringWithFormat:@"%@%@&page=%ld",kBaseUrl,kHistoryUrl, page];
    [NetworkHandle getUpDataWithUrl:url modelClass:NSClassFromString(@"HistoryModel") firstKey:@"data" secondKey:@"list" success:^(NSArray *array) {
        if (page == 1) {
            [self.dataArray removeAllObjects];
        }
        [self.dataArray addObjectsFromArray:array];
        if (self.dataArray.count >= 10 && self.tableVeiw.footer == nil) {
            self.tableVeiw.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
        }
        [self.tableVeiw.footer endRefreshing];
        [self.tableVeiw.header endRefreshing];
        [self.tableVeiw reloadData];
    }];
    
}
- (void)addSubViews
{
    self.title = @"速降历史";
    self.tableVeiw = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight + 24) style:(UITableViewStylePlain)];
    self.tableVeiw.delegate = self;
    self.tableVeiw.dataSource = self;
    [self.view addSubview:self.tableVeiw];
    [self.tableVeiw registerNib:[UINib nibWithNibName:@"HistoryCell" bundle:nil] forCellReuseIdentifier:@"HistoryCell"];
    self.tableVeiw.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableVeiw.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRereshing)];
    
}
- (void)headerRereshing
{
    page = 1;
    [self requestData];

}
- (void)footerRefresh

{
    page++;
    [self requestData];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.dataArray.count > 0) {
        return self.dataArray.count;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    return 136;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HistoryCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.dataArray[indexPath.row];
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
